package com.moshang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.entity.TblGreat;
import com.moshang.blog.mapper.TblGreatMapper;
import com.moshang.blog.service.RedisService;
import com.moshang.blog.service.TblGreatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: blog
 * @description: ${description}
 * @author: moshang
 * @create: 2019-01-04 15:23
 **/
@Service
public class TblGreatServiceImpl extends ServiceImpl<TblGreatMapper, TblGreat> implements TblGreatService {
    @Autowired
    private RedisService redisService;
@Resource
private TblGreatMapper tblGreatMapper;
    @Override
    public void saveDb() {
        //查询点赞详情
        List<TblGreat> list=redisService.getLikedDataFromRedis();
        for (TblGreat tblGreat : list) {
              QueryWrapper<TblGreat> param=new QueryWrapper<>();
              param.eq("user_id",tblGreat.getUserId());
            param.eq("article_id",tblGreat.getArticleId());
               TblGreat tblGreat1=tblGreatMapper.selectOne(param);
            if (tblGreat1 == null){
                //没有记录，直接存入
                tblGreat.setCreateTime(new Date());
                tblGreatMapper.insert(tblGreat);
            }else{
                //有记录，需要更新
                tblGreat1.setStatus(tblGreat.getStatus());
                tblGreat1.setUpdateTime(new Date());
                tblGreatMapper.updateById(tblGreat1);
            }
        }

    }
}
